package com.longteng.lesson2.controller;

import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/cookieController")
public class CookieController {

    @RequestMapping("/setCookie")
    public void setCookie(HttpServletResponse response){
        Cookie cookie =new Cookie("xiaowu","xiaowu-test");
        response.addCookie(cookie);
    }
    @RequestMapping("/getCookie")
    public String getCookie(@CookieValue(name = "xiaowu",defaultValue = "a") String value){
        return value;
    }
    @RequestMapping("/getCookieByName")
    public String getCook0ie(HttpServletRequest request, String name){
      Cookie[] cookies  =request.getCookies();
      for(Cookie cookie:cookies){
          if(cookie.getName().equalsIgnoreCase(name)){
              return  cookie.getValue();
          }
      }
      return null;
    }
}
